package com.burgeon.backend.SHOP.service.Impl;

import com.burgeon.backend.SHOP.entity.Share;
import com.burgeon.backend.SHOP.entity.ShareCon;
import com.burgeon.backend.SHOP.mappers.ShareMapper;
import com.burgeon.backend.SHOP.rest.CreateIntegral;
import com.burgeon.backend.SHOP.service.ShareService;
import com.burgeon.backend.SHOP.service.ex.InsertExceptionApp;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

@Service
public class ShareServiceImpl implements ShareService {
    @Autowired
    private ShareMapper shareMapper;
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public Integer insertShare( String token,String phone,String uid) throws InsertExceptionApp{

        Share shareLast= shareMapper.getShareByToken(token);
        if(shareLast != null){
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String time = sdf.format(date);     //当前的时间
            Share shareToday = shareMapper.getShareByTime(token,time,uid);
            if (shareToday!=null){
                throw new InsertExceptionApp("今日已分享过了");
            }
        }

      return insert(token, phone, uid);
    }

    //根据今天时间分享
    @Override
    public HashMap getShareByTime(String token,String uid) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String daytime = sdf.format(date);     //当前的时间
        HashMap data = new HashMap();
        ShareCon shareCon = shareMapper.getSharePoints(token);
        data.put("award",shareCon.getSharePoints());
        if (null == shareMapper.getShareByTime(token,daytime,uid)){
            data.put("switch","N");
        }else {
            data.put("switch","Y");
        }
        return data;
    }

    private Integer insert(String token,String phone,String uid) {

        System.err.println("uid"+uid);

        ShareCon shareCon = shareMapper.getSharePoints(token);

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(date);     //当前的时间

        Share share = new Share();
        share.setUid(uid);
        share.setTime(time);
        share.setBonusPoints(shareCon.getSharePoints());
        share.setToken(token);
        int r = shareMapper.insertShare(share);

        if(r>0){
            Info info = GetInfo(token);
            CreateIntegral g = new CreateIntegral(info.getUrl(), info.getAccount(), info.getPassword());
            try {
                g.getData(phone, "-"+share.getBonusPoints());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return r;
    }

    private Info GetInfo(String token) {
        return accountMapper.GetTokenData(token);
    }
}
